// pages/My/ bank-card.js
const Controllers = require('../controllers/ControllerMy.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      content: '',
      images: '',
      imageArr: []
    }
  },
  
  suggestAdd() {
    var that = this;
    var formData = that.data.formData;

    if (!formData.content) {
      wx.showToast({
        title: '请输入反馈内容',
        icon: 'none'
      });
      return;
    }
    if (that.data.isLoading) {
      return;
    }
    wx.showLoading();
    that.data.isLoading = true;
    var images = formData.imageArr.join(',');
    var params = { ...formData, images };
    delete params.imageArr;
    Controllers.suggestAdd(params).then(res => {
      console.log('suggestAdd--res==', res);
      const { code, data, desc } = res;
      wx.hideLoading();
      wx.showToast({
        title: desc,
        icon: code === 1 ? 'success' : 'none'
      });
      if (code === 1) {
        setTimeout(() => {
          wx.navigateBack();
        }, 1000);
      } else {
        that.data.isLoading = false;
      }
    })
  },
  getInput(e) {
    console.log('getInput--e==', e);
    var that = this;
    var name = e.target.id;
    var value = e.detail.value;
    var formData = that.data.formData;
    formData[name] = value;
    that.setData({
      formData
    })
    console.log('that.data.formData==', that.data.formData);
  },
  uploadFile(e) {
    console.log('uploadFile--e==', e);
    var that = this;
    var status = e.currentTarget.dataset.status;
    var type = e.currentTarget.dataset.type;
    var formData = that.data.formData;
    var count = 1;
    if (!status) { return }
    app.uploadFile(1, img => {
      console.log('uploadFile--img==', img);
      if (type === 'imageArr') {
        formData.imageArr.push(img);
      } else {
        formData[type] = img;
      }
      that.setData({
        formData
      })
    })
  },
  closeImg(e) {
    console.log('closeImg--e==', e);
    var that = this;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    var imageArr = that.data.formData.imageArr || [];

    if (index > imageArr.length - 1) {
      return;
    }
    wx.showModal({
      title: '',
      content: '确定要删除该图片？',
      success(res) {
        if (res.confirm) {
          imageArr.splice(index, 1);
          that.data.formData.imageArr = imageArr;
          that.setData({
            formData: that.data.formData
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})